function apt_install {
    echo Checking for $1
    problem=$(dpkg -s $1 2> /dev/null |grep installed)
    if [ "" == "$problem" ]; then
	echo "No $1. Setting up $1 ..."
	sudo apt-get install $1 > /dev/null
    else
	echo "$1 found, skipping"
    fi
    echo ""
}

function pip_install {
  echo Checking for $1
  problem=$(pip freeze | grep $1)
  if [ "" == "$problem" ]; then
  	echo "No $1. Setting up $1 .."
  	sudo pip install $1
  else
	echo "$1 found, skipping"
  fi	 
  echo ""
}

function get_property {
awk -F= -v ref=$1 'match($0, ref) {print $2}' ./config.properties
}

function blacklist {
	present=$(cat /etc/modprobe.d/blacklist | grep $1)
	if [ "x$present" == "x" ]; then
		sudo echo "blacklist $1" >> ./blacklist
	fi
}

#install pip
apt_install python-pip

#install pyusb
pip_install pyusb
if [ -f /etc/modprobe.d/blacklist ]; then
    cp /etc/modprobe.d/blacklist ./blacklist
fi
blacklist lirc_atiusb
blacklist ati_remote
sudo cp ./blacklist /etc/modprobe.d/blacklist

#install mysql
mysql_root_password=`get_property "MYSQL_ROOT_PASSWORD"`
sudo debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password password $mysql_root_password'
sudo debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password_again password $mysql_root_password'
apt_install mysql-server
mysql_root_password=""

#install python-mysqldb
apt_install python-mysqldb

#install django
version=`django-admin.py --version`
if [ "x$version" == "x" ]; then
    version="None"
fi

echo "Django Version: $version"
if [ $version == "1.4.2" ]; then
    echo "Django version is current ..."
else
    echo "Django either not found or not current, updating ..."
    wget https://www.djangoproject.com/download/1.4.2/tarball/ -O Django-1.4.2.tar.gz
    tar -xf Django-1.4.2.tar.gz
    cd Django-1.4.2
    sudo python setup.py install > django_install_log
    cd ..
    rm Django-1.4.2.tar.gz
    rm -rf Django-1.4.2
fi
echo ""

#install apache
apt_install apache2
apt_install libapache2-mod-wsgi

#clean
echo "Cleaning ..."
sudo rm -rf home_automation
echo ""

#create project
echo "Creating project ..."
django-admin.py startproject home_automation
echo ""

#create app
cd home_automation
echo "Creating app ..."
python manage.py startapp jarvis
echo ""
cd ..

#Copy files
echo "Configuring app ..."
cp python/home_automation_project/* home_automation/home_automation
cp python/jarvis_app/* home_automation/jarvis
cp -rf static home_automation/jarvis/static
cp -rf templates home_automation/jarvis/templates
echo ""

#collect static files, move to apache distro
echo "Consolidating static files"
sudo python home_automation/manage.py collectstatic --noinput > /dev/null
echo ""

#Set up database
echo "Setting up Database"
mysql_root_password=`get_property "MYSQL_ROOT_PASSWORD"`
mysql_db_name=`get_property "MYSQL_DB_NAME"`
mysql_app_user=`get_property "MYSQL_APP_USER"`
mysql_app_password=`get_property "MYSQL_APP_PASSWORD"`

#Test if database is there
echo use $mysql_db_name | mysql -u root -p$mysql_root_password 2> error.tmp
db_exists=`cat error.tmp`
rm -rf error.tmp

#Test if user exists
user_exists=`echo "use mysql; select user from user where User='jarvis' and Host='localhost'" | mysql -u root -padmin`
 
#Determine if it should create the database
if [ "x$db_exists" == "x" ]; then
	mysql_string=""
else
	mysql_string="CREATE DATABASE $mysql_db_name; "
fi

#Determine if the user should be created
if [ "x$user_exists" == "x" ]; then
    mysql_string=$mysql_string"CREATE USER '$mysql_app_user'@'localhost' IDENTIFIED BY '$mysql_app_password'; "
else
    mysql_string=$mysql_string"SET PASSWORD FOR '$mysql_app_user'@'localhost' = PASSWORD('$mysql_app_password');"
fi

#Create database, sync db with project
mysql_string=$mysql_string"GRANT ALL ON $mysql_db_name.* TO '$mysql_app_user'@'localhost'" 
echo $mysql_string| mysql -u root -p$mysql_root_password
python home_automation/manage.py syncdb
echo ""

#Configure apache
echo "Configuring apache..."
echo "WSGIScriptAlias /  `pwd`/home_automation/ha.wsgi" > httpd.conf
sudo cp httpd.conf /etc/apache2/httpd.conf
rm -rf httpd.conf
echo ""

echo "Creating WSGI configuration file"
#Create ha.wsgi
touch `pwd`/home_automation/ha.wsgi
chown `whoami` `pwd`/home_automation/ha.wsgi
echo "import os, sys" > `pwd`/home_automation/ha.wsgi
echo "sys.path.append('`pwd`/home_automation')" >> `pwd`/home_automation/ha.wsgi
echo "os.environ['DJANGO_SETTINGS_MODULE'] = 'home_automation.settings'" >> `pwd`/home_automation/ha.wsgi
echo "import django.core.handlers.wsgi" >> `pwd`/home_automation/ha.wsgi
echo "application = django.core.handlers.wsgi.WSGIHandler()">> `pwd`/home_automation/ha.wsgi
echo ""

#Restart apache
echo "Restarting apache"
sudo /etc/init.d/apache2 restart
echo ""

echo "Setup complete, site available to 127.0.0.1/jarvis"



